#include <moying_comm_lib/request.h>
#include <moying_proto/robot_basics.pb.h>
#include <moying_proto/robot_api.pb.h>
#include <moying_proto_test/pack_log.h>

// 直接request
void robot_info_callback(const moying_proto::GetRobotInfoService_Request &req, const moying_proto::GetRobotInfoService_Response &res)
{
    std::cout<<"Robot with id="<<req.robot_id()<<" has name "<<res.info().name()<<std::endl;
}
void requestSimulation(MoyingCommuRequestPtr obj){

    unsigned int id = 0;
    while(1){
        moying_proto::GetRobotInfoService_Request robot_info_req;
        robot_info_req.set_robot_id(id++);
        std::function<void(const moying_proto::GetRobotInfoService_Request &req, const moying_proto::GetRobotInfoService_Response &res)> 
               abc = robot_info_callback;
        obj->requestCB("test_robot_info_req", robot_info_req, robot_info_callback);
        std::cout<<"Asking info for robot id "<<id<<std::endl;
        SLEEP(1000);
    }
}

//在class中request
class ClassTest
{
  public:
    ClassTest(MoyingCommuRequestPtr &req)
    :req_(req)
    {
      requestSimulation();
    }

    void requestSimulation()
    {
        unsigned int id = 0;
        while(1){
            moying_proto::GetRobotInfoService_Request robot_info_req;
            robot_info_req.set_robot_id(id++);

            // 使用回调函数
            // req_->request("test_robot_info_req", robot_info_req, &ClassTest::robot_info_callback, this);

            // 发送请求，但是不等待也不回调
            // req_->sendRequest("test_robot_info_req", robot_info_req);

            // 或直接在当前线程等待结果
            // moying_proto::GetRobotInfoService_Response robot_info_res;
            // bool ret = req_->requestAndWait("test_robot_info_req", robot_info_req, robot_info_res, 1000);
            // if(ret)
            //     std::cout<<"ClassTest Asking info for robot id "<<robot_info_req.robot_id()<<", response with name "<<robot_info_res.info().name()<<std::endl;
            // else
            //     std::cout<<"Failed"<<std::endl<<std::endl;

            moying_proto::DummyService_Request dummy_req;
            dummy_req.set_foo("Foo_" + std::to_string(id));
            req_->request("test_dummy_req", dummy_req, &ClassTest::dummy_callback, this);
            SLEEP(1000);
            std::cout<<"send request"<<std::endl;
        }
    }
private:
    void robot_info_callback(const moying_proto::GetRobotInfoService_Request &req, const moying_proto::GetRobotInfoService_Response &res)
    {
        std::cout<<"Robot with id="<<req.robot_id()<<" has name "<<res.info().name()<<std::endl;
    }

    void dummy_callback(const moying_proto::DummyService_Request &req, const moying_proto::DummyService_Response &res)
    {
        std::cout<<"Dummy "<<req.foo()<<", response "<<res.bar()<<std::endl;
    }
    MoyingCommuRequestPtr req_;
};


int main()
{
    std::string sch_root_dir = "./";
    // log4cplus::Initializer initializer;
    // std::string prop_path(sch_root_dir);
    // prop_path += "configs/req_logger.properties";
    // log4cplus::PropertyConfigurator::doConfigure(prop_path); 
    // logger = std::make_shared<log4cplus::Logger>(log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main")));

    MoyingCommuRequestPtr req = std::make_shared<MoyingCommuRequest>("127.0.0.1", 50005, "req");//create communication object, essential
    req->setLogCall(&commLogCall, MoyingCommuLogLevels::TRACE, true);//设置回调，详见logCall
    req->setPrintPackCall(&packLogCall); //设True打印所有收发数据至console，设false不打印
    req->setDiscardPacketBeforeConntect(true);
    req->run();                                 //essential, start up communication threads

    // requestSimulation(req);
    ClassTest class_test(req);
    return 0;
}

